﻿using System;
using System.Net;
using System.Windows.Input;

namespace MyWcfRestClient
{
    class ApiTestViewModel : ViewModelBase
    {
        private ServiceInfo myServiceInfo = new ServiceInfo(ServiceInfo.DefaultServiceAddress);

        public string BaseAddress { get { return myServiceInfo.BaseAddress; } }


        private string apiString;
        public string ApiString
        {
            get
            {
                return apiString;
            }
            set
            {
                if (value != apiString)
                {
                    apiString = value;
                    ReportPropertyChanged("ApiString");
                }
            }
        }

        private string requestString;
        public string RequestString
        {
            get
            {
                return requestString;
            }
            set
            {
                if (value != requestString)
                {
                    requestString = value;
                    ReportPropertyChanged("RequestString");
                }
            }
        }

        private string postBody;
        public string PostBody
        {
            get
            {
                return postBody;
            }
            set
            {
                if (value != postBody)
                {
                    postBody = value;
                    ReportPropertyChanged("PostBody");
                }
            }
        }

        public string[] RequestMethods { get { return new string[] { "GET", "POST" }; } }
        public string requestMethod = "GET";
        public string RequestMethod
        {
            get
            {
                return requestMethod;
            }
            set
            {
                if (value != requestMethod)
                {
                    requestMethod = value;
                    ReportPropertyChanged("RequestMethod");
                }
            }
        }

        private string outputString;
        public string OutputString
        {
            get
            {
                return outputString;
            }
            set
            {
                if (value != outputString)
                {
                    outputString = value;
                    ReportPropertyChanged("OutputString");
                }
            }
        }

        public ICommand SendRequestCommand
        {
            get
            {
                return new GenericCommand
                {
                    ExecuteCallback = delegate
                    {
                        SendRequest();
                    }
                };
            }
        }

        private void SendRequest()
        {
            string requestFullUrl = BaseAddress + "/" + ApiString;
            if (!string.IsNullOrWhiteSpace(RequestString))
            {
                requestFullUrl += "?" + RequestString;
            }

            HttpHelper myHttpHelper = new HttpHelper();
            try
            {
                HttpWebResponse response = null;
                if ("GET" == RequestMethod)
                {
                    response = myHttpHelper.GetRequest(requestFullUrl);
                }
                else
                {
                    response = myHttpHelper.PostTextData(requestFullUrl, PostBody);
                }

                if (null != response)
                {
                    string responseString = myHttpHelper.GetResponseContentString(response);
                    OutputString = string.Format("Succeed!\nRequestUri: {0}\n{1}", requestFullUrl, responseString);
                }
                else
                {
                    OutputString = string.Format("Failed!\nRequestUri: {0}\nResponse is empty!", requestFullUrl);
                }
            }
            catch (Exception ex)
            {
                OutputString = string.Format("Failed!\nRequest Uri: {0}\n{1}", requestFullUrl, ex.Message);
            }
        }

    }
}
